﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;

public partial class School_Manage_SchoolEduManage : System.Web.UI.Page
{
    public int currSchoolYear = 0, currTerm = 0;
    protected  int currentOpenEXAM = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            currentOpenEXAM = SchoolCookie.ExamID;
            Bind();
        }
    }
    
    //private void InitYearAndTerm()
    //{
    //    if (CommonScore.SelectSohoolInfo() != null)
    //    {
    //        currSchoolYear = CommonScore.SelectSohoolInfo().SchoolYear;
    //        currTerm = CommonScore.SelectSohoolInfo().SchoolTerm;
    //    }
    //}

    /// <summary>
    /// 获取学校年级段信息
    /// </summary>
    private string InitGradesInfo()
    {
        string s = "";
        ISchoolInfoLogic logic = Container<ISchoolInfoLogic>.Instance;
        Hashtable table = new Hashtable();
        table.Add("SchoolMark", CommonScore.GetSchoolMark);
        List<SchoolInfo> list = (List<SchoolInfo>)logic.FindAll(table);

        foreach (SchoolInfo item in list)
        {
            switch (item.SchoolOpt)
            {
                case 1://小学
                    s = "1,2,3,4,5,6";
                    break;
                case 2://初中 
                    s = "7,8,9";
                    break;
                case 3://高中 
                    s = "10,11,12";
                    break;
                case 4://小+初 
                    s = "1,2,3,4,5,6,7,8,9";
                    break;
                case 5://初+高 
                    s = "7,8,9,10,11,12";
                    break;
                default://小+初+高
                    s = "1,2,3,4,5,6,7,8,9,10,11,12";
                    break;
            }
        }
        return "(" + s + ")";
      
    }


    //初始化 repeat控件，集成显示教师关联信息
    protected void btnBind_Click(object sender, EventArgs e)
    {
        Bind();
    }
    private void Bind()
    {
        currentOpenEXAM = SchoolCookie.ExamID;
        IExamsEducationLogic eduExamLogic = Container<IExamsEducationLogic>.Instance;
        int areaId = CommonScore.GetAreaID;

        string grades = InitGradesInfo();
        List<VExamsEducation> list = (List<VExamsEducation>)eduExamLogic.SelectSchoolEduExam(areaId, grades,CommonScore.GetSchoolMark);
        CommonScore.ConvertEduExamHtmlCode(ref list);

        List<VExamsEducation> targetList = new List<VExamsEducation>();
        currSchoolYear = CommonScore.SelectSohoolInfo().SchoolYear;
        currTerm = CommonScore.SelectSohoolInfo().SchoolTerm;

        int year = 0, term = 0;
        if (string.IsNullOrEmpty(txtYear.Text))
        {
            if (currSchoolYear > 0 && currTerm > 0)
            {
                year = currSchoolYear;
                term = currTerm;
            }
        }
        else
        {
            int.TryParse(txtYear.Text, out year);
            int.TryParse(txtTerm.Text, out term);
        }
        if (year > 0)
        {
            if (term > 0)
            {
                foreach (VExamsEducation item in list)
                {
                    if (item.SchoolYear == year && item.Term == term)
                        targetList.Add(item);
                }
            }
            else
            {
                foreach (VExamsEducation item in list)
                {
                    if (item.SchoolYear == year)
                        targetList.Add(item);
                }
            }
            Repeater1.DataSource = targetList;
        }
        else
        {
            Repeater1.DataSource = list;
        }
        //if(targetList.Count > 0)
        //    Repeater1.DataSource = targetList;
        //else
        //    Repeater1.DataSource = list;
        Repeater1.DataBind();
    }




    /// <summary>
    /// 过滤查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        int areaId = CommonScore.GetAreaID;
        string grades = InitGradesInfo();
        IExamsEducationLogic eduExamLogic = Container<IExamsEducationLogic>.Instance;
        List<VExamsEducation> listEduExam = (List<VExamsEducation>)eduExamLogic.SelectSchoolEduExam(areaId, grades, CommonScore.GetSchoolMark);
        CommonScore.ConvertEduExamHtmlCode(ref listEduExam);
        if (listEduExam == null || listEduExam.Count == 0)
            return;

        int year = -1, term = -1, grade = -1, state = -1;
        string s = "";
        s = txtYear.Text;
        if (!String.IsNullOrEmpty(s))
            year = Convert.ToInt32(s);
        s = txtTerm.Text;
        if (!String.IsNullOrEmpty(s))
            term = Convert.ToInt32(s);
        s = txtGrade.Text;
        if (!String.IsNullOrEmpty(s))
            grade = Convert.ToInt32(s);
        s = txtState.Text;
        if (!String.IsNullOrEmpty(s))
            state = Convert.ToInt32(s);

        string[] arr = new string[] { "0", "0", "0" };
        if (term > 0)
            arr[0] = "1";
        if (grade > 0)
            arr[1] = "1";
        if (state > -1)
            arr[2] = "1";

        List<VExamsEducation> newList = new List<VExamsEducation>();
        List<VExamsEducation> targetList = new List<VExamsEducation>();
       
        if (year > 0)
        {
            foreach (VExamsEducation item in listEduExam)
            {
                if (item.SchoolYear == year)
                    targetList.Add(item);
            }
        }
        else
        {
            targetList = listEduExam;
        }
        newList = CommonScore.FilterTermGradeState(String.Join("", arr), targetList, term, grade, state);

        currentOpenEXAM = SchoolCookie.ExamID;
        Repeater1.DataSource = newList;
        Repeater1.DataBind();

    }

    /// <summary>
    /// 上报
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int UpReportEduExam(int examId)
    {
      //  IExamsEducationLogic eduExamLogic = Container<IExamsEducationLogic>.Instance;
      //  int n = eduExamLogic.UpReportExam(examId);
        IExamClassEducationLogic logic = Container<IExamClassEducationLogic>.Instance;
        int n = logic.UpdateUpReport(examId,CommonScore.GetSchoolMark);
        return n;
    }


    #region 学年、学期、年级、状态的转换
    public string ConvertGrade(int grade)
    {
        return CommonScore.ConvertGradeNum(grade);
    }
    public string ConvertTerm(short term)
    {
        return CommonScore.ConvertTerm(term);
    }
    public string ConvertClassType(int classType)
    {
        return CommonScore.ConvertClassType(classType);
    }
    public string ConvertState(int status)
    {
        int state = 1;
        if (status > 0)
            state = 2; 
        
        return CommonScore.ConvertTestStatusForSchool(state);
    }
    #endregion

    protected void btnSetCookie_Click(object sender, EventArgs e)
    {
        SetCookie(txtExamId.Text, txtExamName.Text, txtGrade.Text, txtClassType.Text, txtCanUpload.Text);
    }


    private void SetCookie(string examId, string examName,string grade,string classType,string canupload)
    {
        int nExamId = 0, nGrade = 0, nClassType = 0, canup = -1; ;
        if (!String.IsNullOrEmpty(examId))
        {
            if (int.TryParse(examId, out nExamId))
                nExamId = Convert.ToInt32(examId);
        }
        if (!String.IsNullOrEmpty(grade))
        {
            if (int.TryParse(grade, out nGrade))
                nGrade = Convert.ToInt32(grade);
        }
        if (!String.IsNullOrEmpty(classType))
        {
            if (int.TryParse(classType, out nClassType))
                nClassType = Convert.ToInt32(classType);
        }
        if (!String.IsNullOrEmpty(canupload))
        {
            if (int.TryParse(canupload, out canup))
                canup = Convert.ToInt32(canupload);
        }
        if (nExamId > 0 && nGrade > 0)
        {
            currentOpenEXAM = nExamId;
            SchoolCookie.ClearExamInfo();
            SchoolCookie.SetOpenedExam(1, nExamId, examName, nGrade, nClassType, canup);
            
        }

        //Repeater1.DataSource = listEduExam;
        //Repeater1.DataBind();
        Bind();
    }

    /// <summary>
    /// 设置cookie
    /// </summary>
    /// <param name="examId"></param>
    /// <param name="examName"></param>
    /// <param name="grade"></param>
    /// <param name="classType"></param>
    [System.Web.Services.WebMethod]
    public static void SetCookie2(string examId, string examName, string grade, string classType, string canupload)
    {
        int nExamId = 0, nGrade = 0, nClassType = 0, canup = -1; ;
        if (!String.IsNullOrEmpty(examId))
        {
            if (int.TryParse(examId, out nExamId))
                nExamId = Convert.ToInt32(examId);
        }
        if (!String.IsNullOrEmpty(grade))
        {
            if (int.TryParse(grade, out nGrade))
                nGrade = Convert.ToInt32(grade);
        }
        if (!String.IsNullOrEmpty(classType))
        {
            if (int.TryParse(classType, out nClassType))
                nClassType = Convert.ToInt32(classType);
        }
        if (!String.IsNullOrEmpty(canupload))
        {
            if (int.TryParse(canupload, out canup))
                canup = Convert.ToInt32(canupload);
        }
        if (nExamId > 0 && nGrade > 0)
        {
            SchoolCookie.ClearExamInfo();
            SchoolCookie.SetOpenedExam(1, nExamId, examName, nGrade, nClassType, canup);
         //   currentOpenEXAM = nExamId;
        }
    }


    /// <summary>
    /// 获取打开的考试名
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    public string GetOpenedExamName()
    {
        string examName = SchoolCookie.ExamName;
        return examName;
    }


    public string GetStateColor(int status)
    {
        if (status == 0)
            return "red";
        else
            return "";
    }


}
